package com.yaoyong.yy_demo.module.quartz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yaoyong.yy_demo.module.quartz.entity.DemoQuartzJob;
import com.yaoyong.yy_demo.module.quartz.service.IDemoQuartzJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 定时任务表 前端控制器
 * </p>
 *
 * @author yy
 * @since 2021-01-06
 */
@RestController
@RequestMapping("/quartz")
@Tag(name = "quartz演示controller", description = "定时任务接口")
public class DemoQuartzJobController {

    @Autowired
    private IDemoQuartzJobService demoQuartzJobService;

    @Operation(summary = "添加定时任务接口",description = "添加一个定时任务")
    @PostMapping(value = "/add")
    public ResponseEntity<String> add(@RequestBody DemoQuartzJob quartzJob) {
        try {
            quartzJob.setId(null);
            quartzJob.setStatus(0);
            quartzJob.setCreateTime(LocalDateTime.now());
            List<DemoQuartzJob> list = this.demoQuartzJobService.findByJobClassName(quartzJob.getJobClassName());
            if (list != null && list.size() > 0) {
                return ResponseEntity.ok("该定时任务类名已存在");
            }
            if (this.demoQuartzJobService.saveAndScheduleJob(quartzJob)) {
                return ResponseEntity.ok("创建定时任务成功");
            }
            return ResponseEntity.ok("更新定时任务失败!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @Operation(summary = "修改定时任务接口",description = "修改一个定时任务")
    @PutMapping(value = "/edit")
    public ResponseEntity<?> edit(@RequestBody DemoQuartzJob quartzJob) {

        try {
            quartzJob.setUpdateTime(LocalDateTime.now());
            quartzJob.setStatus(0);
            if (this.demoQuartzJobService.editAndScheduleJob(quartzJob)) {
                return ResponseEntity.ok("更新定时任务成功!");
            }
            return ResponseEntity.ok("更新定时任务失败!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

    }

    @Operation(summary = "通过id删除任务接口",description = "通过id删除一个定时任务")
    @DeleteMapping(value = "/delete")
    @Parameter(name = "id",description = "任务ID")
    public ResponseEntity<String> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            DemoQuartzJob quartzJob = this.demoQuartzJobService.getById(id);
            if (quartzJob == null) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).body("未找到对应实体");
            }
            if (this.demoQuartzJobService.deleteAndStopJob(quartzJob)) {
                return ResponseEntity.ok("删除定时任务成功!");
            } else {
                return ResponseEntity.ok("删除定时任务失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @GetMapping(value = "/queryById")
    @Operation(summary = "通过id查询任务接口",description = "通过id查询一个定时任务")
    @Parameter(name = "id",description = "任务ID")
    public ResponseEntity<DemoQuartzJob> queryById(@RequestParam(name = "id", required = true) String id) {
        try {
            DemoQuartzJob quartzJob = this.demoQuartzJobService.getById(id);
            return ResponseEntity.ok(quartzJob);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @GetMapping(value = "/pause")
    @Operation(summary = "暂停定时任务接口",description = "暂停一个定时任务")
    @Parameter(name = "jobClassName",description = "任务类名")
    public ResponseEntity<Object> pauseJob(@RequestParam(name = "jobClassName", required = true) String jobClassName) {
        try {
            DemoQuartzJob quartzJob = this.demoQuartzJobService.getOne(new LambdaQueryWrapper<DemoQuartzJob>().eq(DemoQuartzJob::getJobClassName, jobClassName));
            if (quartzJob == null) {
                return ResponseEntity.ok("定时任务不存在！");
            }
            this.demoQuartzJobService.pause(quartzJob);
            return ResponseEntity.ok("暂停定时任务成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @GetMapping(value = "/resume")
    @Operation(summary = "启动定时任务接口",description = "启动一个定时任务")
    @Parameter(name = "jobClassName",description = "任务类名")
    public ResponseEntity<Object> resumeJob(@RequestParam(name = "jobClassName", required = true) String jobClassName) {
        try {
            DemoQuartzJob quartzJob = this.demoQuartzJobService.getOne(new LambdaQueryWrapper<DemoQuartzJob>().eq(DemoQuartzJob::getJobClassName, jobClassName));
            if (quartzJob == null) {
                return ResponseEntity.ok("定时任务不存在！");
            }
            this.demoQuartzJobService.resumeJob(quartzJob);
            return ResponseEntity.ok("恢复定时任务成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}

